package com.company.project.modular.caigou.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.company.project.core.base.controller.BaseController;
import com.company.project.core.base.tips.ErrorTip;
import com.company.project.core.log.LogObjectHolder;
import com.company.project.core.support.DateTime;
import com.company.project.modular.system.model.Audit;
import com.company.project.modular.system.model.Buy;
import com.company.project.modular.system.service.IAuditService;
import com.company.project.modular.system.service.IBuyService;
import com.company.project.modular.system.warpper.AuditWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 物质报买控制器
 *
 * @author lsf
 * @Date 2018-10-30 15:19:22
 */
@Controller
@RequestMapping("/buy")
public class BuyController extends BaseController {

    private String PREFIX = "/caigou/buy/";

    @Reference
    private IBuyService buyService;
    @Reference
    private IAuditService auditService;
    /**
     * 跳转到物质报买首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "buy.html";
    }

    /**
     * 跳转到添加物质报买
     */
    @RequestMapping("/buy_add")
    public String buyAdd() {
        return PREFIX + "buy_add.html";
    }

    /**
     * 跳转到修改物质报买
     */
    @RequestMapping("/buy_update/{buyId}")
    public String buyUpdate(@PathVariable Integer buyId, Model model) {
        Buy buy = buyService.selectById(buyId);
        model.addAttribute("item",buy);
        LogObjectHolder.me().set(buy);
        return PREFIX + "buy_edit.html";
    }

    /**
     * 获取物质报买列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
       /* return buyService.selectList(null);*/
        List<Map<String, Object>> buy = buyService.list(condition);
        return super.warpObject(new AuditWarpper(buy));
    }

    /**
     * 新增物质报买,同时新增审核单
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Buy buy, Audit audit, BindingResult bindingResult) {
        Date date=new Date();   //获取当前系统时间
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time1 = format.format(date);
        Date time = Timestamp.valueOf(time1);//把String 类型转换

        String maxNumber = buyService.findByMaxNumber().get(0).getBuyId();
        if("null".equals(maxNumber)){
            int a = 10001;
            String number = String.valueOf(a);
            buy.setBuyId(number);
        }else {
            int a = Integer.valueOf(maxNumber)+1;
            String number = String.valueOf(a);
            buy.setBuyId(number);
        }
        buy.setBuyDate(time);
        buyService.insert(buy);

        /*新增审核单*/
        String maxNumberAudit = String.valueOf(auditService.findByMaxNumber().get(0).getAuditId());
        /*审核单号*/
        if("null".equals(maxNumberAudit)){
            int a = 30001;
            String number = String.valueOf(a);
            audit.setAuditId(number);
        }else {
            int a = Integer.valueOf(maxNumberAudit)+1;
            String number = String.valueOf(a);
            audit.setAuditId(number);
        }
        /*将buyId存到auditPurId里面*/
        if("null".equals(maxNumber)){
            audit.setAuditPurId("10001");
        }else{
            int a = Integer.valueOf(maxNumber)+1;
            String number = String.valueOf(a);
            audit.setAuditPurId(number);
        }

        audit.setAuditPersion("等待审核");
        audit.setAuditPass(3);
        audit.setAuditState(1);
        String auditName = buy.getBuyName();
        /*对照品名称==buyNmae==报买名称*/
        audit.setAuditName(buy.getBuyName());
        audit.setAuditDate(time);
        auditService.insert(audit);
        return new ErrorTip(200,"添加成功");

        /*return SUCCESS_TIP;*/
    }

    /**
     * 删除物质报买
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer buyId) {
        buyService.deleteById(buyId);
        return SUCCESS_TIP;
    }

    /**
     * 修改物质报买
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Buy buy,BindingResult bindingResult) {
        buyService.updateById(buy);
        return SUCCESS_TIP;
    }

    /**
     * 物质报买详情
     */
    @RequestMapping(value = "/detail/{buyId}")
    @ResponseBody
    public Object detail(@PathVariable("buyId") Integer buyId) {
        return buyService.selectById(buyId);
    }
}
